package com.example.zxk.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private CustomRecycleView mRecyclerView;
    private GalleryAdapter mAdapter;
    private List<Integer> mDatas;
    private Button qrcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        qrcode = (Button) findViewById(R.id.qrcode);
        qrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
                intentIntegrator.setCaptureActivity(MyCaptureActivity.class);
                intentIntegrator.initiateScan();
            }
        });

//        Class<?> cl = this.getClass();
//        for (Method m: cl.getDeclaredMethods()) {
//            Override o = m.getAnnotation(Override.class);
//            if (o != null){
//
//            }
//        }
//        initDatas();
//        mRecyclerView = (CustomRecycleView) findViewById(R.id.id_recyclerview_horizontal);
//        mAdapter = new GalleryAdapter(this,mDatas);
//        mRecyclerView.setItemAnimator(new DefaultItemAnimator());


        //设置布局管理器
        //瀑布流式
//        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL);
//        //线性布局
//        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        mRecyclerView.setLayoutManager(linearLayoutManager);
////        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
//        mRecyclerView.setAdapter(mAdapter);
//        mAdapter.setOnItemClickListener(new GalleryAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
////                mRecyclerView.checkAutoAdjust(position);
//                mRecyclerView.smoothToCenter(position);
//            }
//
//            @Override
//            public void onItemLongClick(View view, int position) {
//                mDatas.remove(position);
////                mAdapter.setDatas(mDatas);
//                mAdapter.notifyItemRemoved(position);
//            }
//        });
//
//        mAdapter.setOnItemSelectListener(new GalleryAdapter.OnItemSelectListener() {
//            @Override
//            public void onItemSelect(View view, int position) {
////                linearLayoutManager.scrollToPositionWithOffset(position,350);
//                mRecyclerView.smoothToCenter(position);
//            }
//        });
//        mRecyclerView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                Log.i("abc", "hasfocus:" + hasFocus);
//                if (hasFocus) {
//                    if (mRecyclerView.getChildCount() > 0) {
//                        linearLayoutManager.scrollToPositionWithOffset(0, 0);
//                        mRecyclerView.getChildAt(0).requestFocus();
//                    }
//                }
//            }
//        });

        /*mRecyclerView.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.i("abc","onKey-----:"+keyCode);
                if(keyCode==22){
                    mRecyclerView.scrollBy(100, 0);
//                    mRecyclerView.getChildAt(2).requestFocus();
//                    mRecyclerView.scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition()+1);
                }else if(keyCode==21){

                }
                return false;
            }
        });*///item获得焦点后，rv不能响应OnKeyListener事件了
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "取消扫描", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "扫描内容:" + result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }


    }

    private void initDatas()
    {
        mDatas = new ArrayList<Integer>(Arrays.asList(R.mipmap.osd_blue_hl,
                R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
                R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.osd_hd_hl));
    }
}
